<?php 
error_reporting(NULL);
ini_set('display_errors','Off');
include_once('HttpClient.class.php'); 
include_once('core.php');
include_once('connect.php'); 
session_start();

$mod = $_POST['mod'];
$account = $_POST["client"];
if(empty($account))
{
	return;
}
if (empty($mod))
{
	return;
}
if ($mod=='GetCookie')
{
	$http = new HttpClient("zc.qq.com");
	$http->setreferer("http://zc.qq.com/chs/index.html");
	$http->setUserAgent($_SERVER['HTTP_USER_AGENT']);
	$http->setPersistCookies(true);
	$result = $http->get("/cgi-bin/chs/numreg/init?r=0.7321141598932543&cookieCode=undefined");
	$cookie = $http->getCookies();
	$obj = null;
	$obj-> code = 0;
	$obj-> cookie = $cookie;
	echo json_encode($obj);
}
elseif ($mod == "GetViery")
{
	$data = $_POST["data"];
	$data = urlencode($data);
	$data = str_replace("%5C","",$data);
	$data =urldecode($data) ;	
	$obj= null;
	$obj = json_decode($data);
 	if (is_array($obj->cookie)){return;}
       $Proxy = GetProxy();
	$_SESSION['ip'] = $Proxy;
	$pos = strpos($Proxy,":");
	$ProxyIP = substr($Proxy,0,$pos);
	$ProxyPort = substr($Proxy,$pos+1,strlen($Proxy)-$pos);	
	
	//$obj->ip = $Proxy;
	//echo $ProxyIP;
    
	$http = new HttpClient("captcha.qq.com");
	$http->setreferer("http://zc.qq.com/chs/index.html");
	$http->setUserAgent($_SERVER['HTTP_USER_AGENT']);
	$http->setCookies((array)$obj->cookie);

    
  	$http->get("/getimage?aid=1007901&r=0.680727266240865");
	$result = $http->getContent();
	//$result = json_decode($result);
	$obj -> cookie = $http->getCookies();
	sleep(5);
	echo json_encode($obj) . "<br />";
	echo $result;   
}
elseif ($mod == "GetAcc")
{
	$data = $_POST["data"];
	$data = urlencode($data);
	$data = str_replace("%5C","",$data);
	$data =urldecode($data) ;	
	$obj= null;
	$obj = json_decode($data);
	if (empty($obj->code)){return;}
	if (empty($obj->pass)){return;}
	if (empty($obj->rsa)){return;}
	if (empty($obj->nick)){return;}
	if (is_array($obj->cookie)){return;}
	$Proxy = $_SESSION['ip'];
	if(empty($Proxy)) $Proxy = $obj->ip;
	if(empty($Proxy)) $Proxy=GetProxy();
	$pos = strpos ($Proxy,":");
	
	$ProxyIP = substr($Proxy,0,$pos);
	$ProxyPort = substr($Proxy,$pos+1,strlen($Proxy)-$pos);	
	
	$http = new HttpClient("zc.qq.com");
	$http->setreferer("http://zc.qq.com/chs/index.html");
	$http->setUserAgent($_SERVER['HTTP_USER_AGENT']);
	$http->setCookies((array)$obj->cookie);
	$http->SetProxy($ProxyIP,$ProxyPort);
	$body = "&verifycode={code}&qzone_flag=0&country=1&province=15&city=" . rand(1,5) . "&isnongli=0&year=" .   mt_rand(1995,2010) . "&month=" . mt_rand(1,5) . "&day=1&isrunyue=0&password={pass}&nick={nick}&email=false&other_email=false&elevel=1&sex=1&qzdate=&jumpfrom=58030&csloginstatus=1&o8h0r1=q3u5";
	$body =str_replace("{code}",$obj->code,$body);
	$body =str_replace("{pass}",$obj->rsa,$body);
	$body =str_replace("{nick}",$obj->nick,$body);
  	$http->post("http://zc.qq.com/cgi-bin/chs/numreg/get_acc?r=0.5161543698179318",$body);
	$result = $http->getContent();
       // $obj -> body = $body;

	//$result = json_decode($result);
	$obj -> cookie = $http->getCookies();
       $result = json_decode($result);

	if(empty($result))
	{
		$obj -> code = -1;
	}
	else{
	       //$obj -> body = $body;
		$obj -> code = $result->ec;
		if ($obj->code ==0)
		{
			//$obj->uin=$result->uin;
			$obj -> cookie =null;
			$other->save($account.strlen($result->uin),$result->uin."----".$obj->pass);
		}
	}
	//$obj->ip = $Proxy;
	echo json_encode($obj);  
}
elseif ($mod == "SendSms")
{
	$data = $_POST["data"];
	$data = urlencode($data);
	$data = str_replace("%5C","",$data);
	$data =urldecode($data) ;	
	$obj= null;
	$obj = json_decode($data);
	if (empty($obj->tel)){return;}
	if (is_array($obj->cookie)){return;}
	$Proxy = $_SESSION['ip'];
	if(empty($Proxy)) $Proxy = $obj->ip;
	if(empty($Proxy)) $Proxy=GetProxy();
	$pos = strpos($Proxy,":");
	$ProxyIP = substr($Proxy,0,$pos);
	$ProxyPort = substr($Proxy,$pos+1,strlen($Proxy)-$pos);	
	
	$http = new HttpClient("zc.qq.com");
	$http->setreferer("http://zc.qq.com/chs/phone_verify.html?type=0");
	$http->setUserAgent($_SERVER['HTTP_USER_AGENT']);
	$http->SetProxy($ProxyIP,$ProxyPort);
	$http->setCookies((array)$obj->cookie);
	$body = "&telphone={tel}&elevel=3&regType=3&r=0.". rand();
	$body =str_replace("{tel}",$obj->tel,$body);
  	$http->post("http://zc.qq.com/cgi-bin/chs/common/sms_send",$body);
	$result = $http->getContent();
	$result = json_decode($result);	
	$obj -> cookie = $http->getCookies();
	if(empty($result))
	{
		$obj -> code = -1;
	}
	else{
		$obj -> code = $result->ec;
	}
	
	//$obj->ip = $Proxy;
	echo json_encode($obj);  
}
elseif ($mod == "GetAccSms")
{
	$data = $_POST["data"];
	$data = urlencode($data);
	$data = str_replace("%5C","",$data);
	$data =urldecode($data) ;	
	$obj= null;
	$obj = json_decode($data);
	if (empty($obj->code)){return;}
	if (empty($obj->pass)){return;}
	if (empty($obj->nick)){return;}
	if (empty($obj->rsa)){return;}
	if (is_array($obj->cookie)){return;}
	
	if(empty($Proxy)) $Proxy = $obj->ip;
	if(empty($Proxy)) $Proxy=GetProxy();
	$pos = strpos ($Proxy,":");
	if(empty($ProxyIP)) $ProxyIP = substr($Proxy,0,$pos);
	$ProxyPort = substr($Proxy,$pos+1,strlen($Proxy)-$pos);	
	
	//$obj->ip = $Proxy;
	
	$http = new HttpClient("zc.qq.com");
	$http->setreferer("http://zc.qq.com/chs/phone_verify_up.html?type=0");
	$http->setUserAgent($_SERVER['HTTP_USER_AGENT']);
	$http->SetProxy($ProxyIP,$ProxyPort);
	$http->setCookies((array)$obj->cookie);
	$body = $obj->body . "&telphone={tel}&smsvc={code}&phone_flag=0&r=0.7740803437864616";
	$body =str_replace("{code}",$obj->code,$body);
	$body =str_replace("{tel}",$obj->tel,$body);
	$body =str_replace("&elevel=1","&elevel=3",$body);
  	$http->post("http://zc.qq.com/cgi-bin/chs/numreg/get_acc",$body);
	$result = $http->getContent();
	$result = json_decode($result);
	$obj -> cookie = $http->getCookies();
	if(empty($result))
	{
		$obj -> code = -1;
	}
	else{
		$obj -> body = $body;
		$obj -> code = $result->ec;
		if ($obj->code ==0)
		{
			//$obj->uin=$result->uin;
			$obj -> cookie =null;
			$other->save($account.strlen($result->uin),$result->uin."----".$obj->pass);
		}
	}
	echo json_encode($obj);  
}
elseif ($mod == "GetProxy")
{
	$Proxy=GetProxy();

	$pos = strpos ($Proxy,":");
	$ProxyIP = substr($Proxy,0,$pos);
	$ProxyPort = substr($Proxy,$pos+1,strlen($Proxy)-$pos);		
	echo $ProxyIP.$ProxyPort;
}

function GetProxy(){
    //*
	$db = new db();
	$db->Connect("127.0.0.1:3306","root","123456");
	$Proxy = $db->Get();
	if (empty($Proxy))
	{
		return;
	}
	$pos = strpos ($Proxy,":");
	$ProxyIP = substr($Proxy,0,$pos);
	$ProxyPort = substr($Proxy,$pos+1,strlen($Proxy)-$pos);
	if (empty($ProxyIP))
	{
		echo false ;
		return;
	}
	if ($ProxyPort<=0)
	{
		echo false ;
		return;
	}
	$bool = CheckProxy($ProxyIP,$ProxyPort);
	if ($bool)
	{
		return $Proxy;
	}
	else
	{
		GetProxy();
	}
}

function CheckProxy($ip,$port)
{
	$http = new HttpClient("wallet.tenpay.com");
	$http->SetProxy($ip,$port);
	$http->settimeout(5);

	$http->get("http://wallet.tenpay.com/cgi-bin/v1.0/queryqb.cgi");
	$request = $http->getContent();
	$string = new String();
	$result = $string->Between($request,"<ENV_ClientIp>","</ENV_ClientIp>");
	return $result == $ip;
}

?>